import React from 'react';
import { Select } from 'antd';
import { SelectProps } from 'antd/es/select';

interface IProps extends SelectProps<any> {
  options: any[];
  labelName?: string;
  valName?: string;
}

export default function Selection({
  options,
  labelName = 'label',
  valName = 'value',
  placeholder = '请选择',
  ...props
}: IProps) {
  return (
    <Select {...props} placeholder={placeholder}>
      {options.map((item: any) => {
        if (typeof item === 'object') {
          return (
            <Select.Option value={item[valName]} key={item[valName]} option={item}>
              {item[labelName]}
            </Select.Option>
          );
        }
        return (
          <Select.Option value={item} key={item}>
            {item}
          </Select.Option>
        );
      })}
    </Select>
  );
}

Selection.defaultProps = {
  labelName: 'label',
  valName: 'value',
  allowClear: false,
  suffixIcon: undefined,
  placeholder: undefined,
};
